package com.usian.controller;

import com.usian.pojo.Goods;
import com.usian.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private GoodService goodService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Goods> goodsList = goodService.findAll();
        model.addAttribute("goodList", goodsList);
        return "index";
    }

    @RequestMapping("/shenhe")
    public String shenhe(Model model) {
        List<Goods> goodsList = goodService.findList();
        model.addAttribute("goodList", goodsList);
        return "shenhe";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "add";
    }

    @RequestMapping("/add")
    public String add(Goods goods) {
        goodService.add(goods);
        return "redirect:/good/findAll";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
        goodService.delete(id);
        return "redirect:/good/findAll";
    }

    @RequestMapping("/findById")
    public String findById(Model model, Integer id) {
        Goods goods = goodService.findById(id);
        model.addAttribute("goods", goods);
        return "update";
    }

    @RequestMapping("/findBy")
    public String findBy(Model model, Integer id) {
        Goods goods = goodService.findById(id);
        model.addAttribute("goods", goods);
        return "updateshen";
    }

    @RequestMapping("/update")
    public String update(Goods goods) {
        goodService.update(goods);
        return "redirect:/good/findAll";
    }

    @RequestMapping("/updateshen")
    public String updateshen(Goods goods) {
        goodService.updateshen(goods);
        return "redirect:/good/shenhe";
    }

    @RequestMapping("/findRelease")
    public List<Integer> findRelease(){
        return goodService.findRelease();
    }

    @RequestMapping("/shen")
    public String shen(Integer id) {
        goodService.shen(id);
        return "redirect:/good/findAll";
    }
}
